package com.scuvc.dao.impl;

import com.scuvc.dao.BrandInfoDao;
import com.scuvc.entity.BrandInfo;
import com.scuvc.utils.ConnectionFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BrandInfoDaoImpl implements BrandInfoDao {
    private QueryRunner queryRunner = new QueryRunner(ConnectionFactory.getDruidDataSource());
    @Override
    public List<BrandInfo> getAllBrand() {
        String sql = "SELECT * FROM eh_brandInfo";
        List<BrandInfo> list = null ;
        try {
            list = queryRunner.query(sql,new BeanListHandler<BrandInfo>(BrandInfo.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Long getBrandId(String brand_name) {
        String sql = "SELECT id FROM eh_brandInfo WHERE brand_name=?";
        try {
            return queryRunner.query(sql,new ScalarHandler<>(),brand_name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
